# -*- coding: utf8 -*-

from puppy.query import QueryFilter
from puppy.model import Query

class TermExpansionFilter(QueryFilter):
  """Expands original query terms with extra terms."""
  
  def __init__(self, options=""):
    """Constructor for TermExpansionFilter."""
    super(TermExpansionFilter, self).__init__(options)
    self.info = "Expands original query terms with extra terms."
  
  
  def run(self, query):
    """Expands query with new terms.
    
    Extra terms are passed to the filter via its options['terms'] entry.  For convenience, the string returned from options['terms'] has '+' characters where there should be spaces.  This is resolved prior to query expansion.
    
    Parameters:
    
    * query (puppy.model.Opensearch.Query): original query
    
    Returns:
    
    * query (puppy.model.Opensearch.Query): expanded query
    
    """
    original_terms = query.search_terms
    extra_terms = self.options['terms'].replace('+', ' ')
    query.search_terms = " ".join([original_terms, extra_terms.lstrip()])
    return query
    